<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Admin\Controller;

/**
 * 后台视频直播控制器
 */
class LiveController extends AdminController
{

    public function index()
    {
        /* 查询条件初始化 */
        $map = array();
        if (IS_GET) {
            $title = trim(I('get.title'));
            $map['title'] = array('like', "%{$title}%");
            $list = M("Live")->where($map)->field(true)->order('id desc')->select();
        } else {
            $list = $this->lists('Live', $map, 'id desc');
        }
        $this->assign('list', $list);
        // 记录当前列表页的cookie
        Cookie('__forward__', $_SERVER['REQUEST_URI']);

        $this->assign('meta_title','视频直播管理');
        $this->display();
    }

    /* 编辑分类 */
    public function edit()
    {
        $id = I('id', '');
        $info = M('Live')->where('id='.$id)->find();
        $this->assign('info',$info);
        $this->assign('meta_title','视频编辑');
        $this->display();
    }

    /* 新增分类 */
    public function add()
    {
        $this->assign('meta_title','视频添加');
        $this->display();
    }

    public function update()
    {
        $id = I('id', '');
        $title = I('title', '');
        $cover_id = I('cover_id', 0);
        $url = I('url', '');
        $status = I('status', '0');
        $detail = I('detail', '');

        $data['title'] = $title;
        $data['cover_id'] = $cover_id;
        $data['url'] = $url;
        $data['status'] = $status;
        $data['detail'] = $detail;
        $data['update_time'] = time();

        $live = M('Live');

        if (empty($id)) {
            $data['create_time'] = time();
            $res = $live->data($data)->add();
            if ($res) {
                $this->success('添加成功!',U('index'));
            } else {
                $this->error('添加失败!',U('index'));
            }
        } else {
            $where['id'] = $id;
            $count = $live->where($where)->count();
            if ($count) {
                $res = $live->where($where)->data($data)->save();
                if ($res) {
                    $this->success('更新成功',U('index'));
                } else {
                    $this->error('更新失败!',U('index'));
                }
            } else {
                $this->error('更新失败!',U('index'));
            }
        }
    }

    public function active(){
        $id = I('id', '');
        $status = I('status', '');
        $where = array('id' => $id);
        if (!empty($id) && ($status == '1' || $status == '0')) {
            $count = M('Live')->where($where)->count();
            if ($count == 0) {
                $this->error("不存在该记录");
            } else {
                $res = M('Live')->where($where)->data(array('status' => $status))->save();
                if ($res) {
                    $this->success("状态修改成功");
                } else {
                    $this->error("状态修改失败");
                }
            }
        } else {
            $this->error("不存在该记录");
        }
    }


    public function del()
    {
        if (IS_POST) {
            $ids = I('post.id');
            $order = M("Live");

            if (is_array($ids)) {
                foreach ($ids as $id) {
                    $order->where("id='$id'")->delete();
                }
            }
            $this->success("删除成功！");
        } else {
            $id = I('get.id');
            $db = M("Live");
            $status = $db->where("id='$id'")->delete();
            if ($status) {
                $this->success("删除成功！");
            } else {
                $this->error("删除失败！");
            }
        }
    }

}